package com.simulation.web.controller.tech;

import com.simulation.common.core.domain.AjaxResult;
import com.simulation.common.utils.SecurityUtils;
import com.simulation.web.domain.TechTeamMembers;
import com.simulation.web.dto.TechTeamMembersDTO;
import com.simulation.web.service.TeamMemberService;
import com.simulation.web.service.TeamService;
import com.simulation.web.vo.TechTeamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 唐雄杰
 * @Date 2024-08-25 23:13
 **/
@RestController
@RequestMapping("/teamMember")
@Api("团队人员管理")
public class TeamMemberController {
    @Resource
    private TeamMemberService teamMemberService;

    @Resource
    private TeamService teamService;

    @PostMapping("/batchAddTeamMembers")
    public AjaxResult batchAddTeamMembers(@RequestBody List<TechTeamMembers> members) {
        teamMemberService.batchAddTeamMembers(members);
        return AjaxResult.success();
    }

    @PostMapping("/joinTeam")
    public AjaxResult joinTeam(@RequestBody TechTeamMembersDTO techTeamMembersDTO) {
        List<TechTeamMembers> membersList = new ArrayList<>();
        TechTeamVO techTeamVO = teamService.getTeamByTeamCode(techTeamMembersDTO.getTeamCode());
        TechTeamMembers techTeamMembers = new TechTeamMembers();
        techTeamMembers.setTeamId(techTeamVO.getId());
        techTeamMembers.setJoinedTime(LocalDateTime.now());
        techTeamMembers.setUserId(SecurityUtils.getUserId());
        membersList.add(techTeamMembers);
        teamMemberService.batchAddTeamMembers(membersList);
        return AjaxResult.success();
    }

    @GetMapping("/getTeamMembersPaged")
    public AjaxResult getTeamMembersPaged(TechTeamMembersDTO techTeamMembersDTO) {
        return AjaxResult.success(teamMemberService.getTeamMembersPaged(techTeamMembersDTO));
    }

    @PostMapping("/batchDeleteTeamMembers")
    public AjaxResult batchDeleteTeamMembers(@RequestBody TechTeamMembersDTO techTeamMembersDTO) {
        teamMemberService.batchRemoveTeamMembers(techTeamMembersDTO.getTeamId(), techTeamMembersDTO.getMemberIds());
        return AjaxResult.success();
    }

    @GetMapping("/deleteTeamMembers")
    @ApiOperation("单个删除团队成员")
    public AjaxResult deleteTeamMembers(@RequestParam("id") Long id) {
        teamMemberService.deleteMembers(id);
        return AjaxResult.success();
    }

}
